<?php
if(!defined("YUEXUAN_REAL_ROOT_PATH")){
	exit("Access Denied.");
}

/**
 * memcache接口
 * @author tongjun
 *
 */
class ext_memcacheHandle {
    private $_memcache;
    private static $_instance;
    private $_enableCache = true;
    private $_keyCacheTime = array(
    	"friendOnceGameRank" => 3600,
    	"userGameInfo" => 3600,
    	"userCurrentCharacter" => 3600,
    	"userActiveCharacterList" => 3600,
    	"onceGameRank" => 3600,
    	"totalRank" => 3600,
    	"userTopScore" => 3600,
    	"userTodayTopScore" => 3600,
    	"rankMemcacheKeyQueueIndex" => 3600,
    	"rankMemcacheKeyQueueValue" => 3600,
    	"rankMemcacheKeyQueueLock" => 3600,
    	"shopItemList" => 3600,
    	"charInfo" => 3600,
    	"gameAuthID" => 3600,
    	"unreadMsgCount" => 3600,
    	"allMsgCount" => 3600,
    	"unreadMsgList" => 3600,
    	"countryOnceGameRank" => 3600,
    );
    
    private function __construct() {
    	// 连接memcache
		$this->_memcache = new Memcache;
		$logger = ext_logger::getInstance();
		$jsonHandle = new ext_jsonHandle();
		if(!$this->_memcache->connect(
				MEMCACHED_HOST, 
				MEMCACHED_PORT)){
			$logger->debug("连接memcache失败");
			$jsonHandle->error('连接服务器失败!');
		}
    }
    private function __clone() {
    	
    }
    /**
     * 单例模式下取得memcache接口
     * @return array
     */
    public static function getInstance() {
        if(!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    
    /**
     * 添加一个key
     * @param string $identify
     * @param array $condition
     * @param mixd $value
     */
    public function add($identify, $condition=array(), $value="", $cacheTime=null){
    	 $key = $this->createKey($identify, $condition);
    	 $logger = ext_logger::getInstance();
    	 $logger->debug("add添加一个memcache key： ".$key);
    	 if($cacheTime==0){
    	 	return $this->_memcache->add($key, $value);
    	 }else{
    	 	return $this->_memcache->add($key, $value,
    	 			MEMCACHE_COMPRESSED, $cacheTime);
    	 }
    }
    /**
     * 取得数据
     * @param string $identify
     * @param array $condition
     * @return mixed
     */
    public function get($identify, $condition=array()){
    	if(!$this->_enableCache){
    		return null;
    	}
    	$key = $this->createKey($identify, $condition);
    	$logger = ext_logger::getInstance();
    	$logger->debug("get读取memcache数据： ".$key);
    	return $this->_memcache->get($key);
    }
    /**
     * 保存数据
     * @param string $identify
     * @param array $condition
     * @param mixed $value
     */
    public function set($identify, $condition, $value, $cacheTime=null){
    	$key = $this->createKey($identify, $condition);
    	if($cacheTime===null){
    		$cacheTime = $this->_keyCacheTime[$identify];
    	}
    	$logger = ext_logger::getInstance();
    	$logger->debug("set设置memcache数据： ".$key.print_r($value,1));
    	if($cacheTime==0){
    		$this->_memcache->set($key, $value);
    	}else{
    		$this->_memcache->set($key, $value, 
    				MEMCACHE_COMPRESSED, $cacheTime);
    	}
    }
    /**
     * 删除数据
     * @param string $identify
     * @param array $condition
     */
    public function delete($identify, $condition=array()){
    	 $key = $this->createKey($identify, $condition);
    	 $logger = ext_logger::getInstance();
    	 $logger->debug("删除memcache数据： ".$key);
    	 $this->_memcache->delete($key);
    }
    
    /**
     * 增加
     * @param string $identify
     * @param array $condition
     * @param int $value
     */
    public function increment($identify, $condition=array(), $value=1){
    	 $key = $this->createKey($identify, $condition);
    	 $logger = ext_logger::getInstance();
    	 $logger->debug("增加memcache数据： ".$key);
    	 $this->_memcache->increment($key, $value);
    }
    
    /**
     * 减少
     * @param string $identify
     * @param array $condition
     * @param int $value
     */
    public function decrement($identify, $condition=array(), $value=1){
    	 $key = $this->createKey($identify, $condition);
    	 $logger = ext_logger::getInstance();
    	 $logger->debug("减少memcache数据： ".$key);
    	 $this->_memcache->decrement($key, $value);
    }
    
    /**
     * 根据条件创建key
     * @param string $identify
     * @param array $condition
     * @return string
     */
    private function createKey($identify, $condition=array()){
    	if(isset($this->_keyCacheTime[$identify])){
    		$key = $identify;
    		foreach($condition as $k=>$v){
    			$key .= "_".$k."_".$v;
    		}
    		return KVDB_TABLE_PRE.$key;
    	}else{
    		$logger = ext_logger::getInstance();
    		$logger->error($identify."不是有效的memecache key");
    		throw new Exception($identify."不是有效的memecache key");
    	}
    	
    }
}
